package cn.nuc.edu.onlinestore.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.nuc.edu.onlinestore.entity.SysFunction;
import cn.nuc.edu.onlinestore.entity.Sysrole;
import cn.nuc.edu.onlinestore.service.interfaces.IRoleService;
import cn.nuc.edu.onlinestore.util.PageControl;
import cn.nuc.edu.onlinestore.util.Pager;

/**
 * 角色handler
 * @author 王凯
 *
 */
@Controller
@RequestMapping("/role")
public class RoleHandler {
	
	/**
	 * 角色业务逻辑
	 */
	@Autowired
	private IRoleService roleService;
	
	/**
	 * 跳转到添加用户的页面（受WEB-INF保护）
	 * @return
	 */
	@RequestMapping("initadd")
	public String initadd() {
		return "forward:/WEB-INF/views/role/add.jsp";
	}
	
	/**
	 * 初始化修改页面
	 * 
	 */
	@RequestMapping(value = "/toedit/{id}", method = RequestMethod.GET)
	public ModelAndView toedit(@PathVariable("id") Integer id, 
				ModelAndView modelAndView) {

		//获得角色的详细信息
		Sysrole role = new Sysrole();
		role.setRoleid(id);
		role = roleService.detail(role);
		
		//将角色信息放入request范围
		if (role != null) {
			modelAndView.addObject("item", role);
			modelAndView.setViewName("forward:/WEB-INF/views/role/edit.jsp");
		} else {
			modelAndView.addObject("msg", "需要修改的角色不存在。");
			modelAndView.setViewName("forward:/role/roles");
		}
		return modelAndView;
	}
	
	/**
	 * 修改角色信息
	 * 
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.PUT)
	public ModelAndView edit(Sysrole role, ModelAndView modelAndView) {

		//执行修改操作
		Integer rtn = roleService.edit(role);
		if (rtn > 0) {
			modelAndView.setViewName("redirect:/role/roles");
		} else {
			modelAndView.addObject("msg", "保存系统功能失败！");
			modelAndView.setViewName("forward:/WEB-INF/views/role/edit.jsp");
		}
		return modelAndView;
	}

	/**
	 * 保存角色权限
	 */
	@RequestMapping(value = "/saveright", method = RequestMethod.POST)
	public ModelAndView saveright(Integer[] ckrr, Integer roleid, 
			ModelAndView modelAndView) {
		
		//接收角色的功能
		Integer[] funids = ckrr;
		
		//调用业务层方法保存角色的权限
		Integer rtn = roleService.saveright(roleid, funids);
		if(rtn > 0){
			modelAndView.setViewName("redirect:/role/roles");
		}else{
			modelAndView.addObject("msg", "保存角色权限失败");
			modelAndView.setViewName("forward:/WEB-INF/views/role/right.jsp");
		}
		return modelAndView;
	}

	/**
	 * 初始化权限分配页面
	 */
	@RequestMapping(value = "/initrole/{roleid}", method = RequestMethod.GET)
	public ModelAndView initrole(
			@PathVariable(value = "roleid", required = false) Integer roleid, 
			ModelAndView modelAndView) {
		
		//查询具体的角色信息
		Sysrole role = new Sysrole();
		role.setRoleid(roleid);
		role = roleService.detail(role);
		
		//将角色信息保存到request范围内
		modelAndView.addObject("role", role);
		
		//获得角色对应的权限列表并保存到request范围内
		List<SysFunction> list = roleService.initfunlist(role);
		modelAndView.addObject("list", list);
		
		//跳转到权限页面
		modelAndView.setViewName("forward:/WEB-INF/views/role/right.jsp");
		return modelAndView;
	}

	/**
	 * 新增角色
	 */
	@RequestMapping("/add")
	public ModelAndView add(Sysrole role, ModelAndView modelAndView) {
		
		//执行业务层的添加逻辑
		Integer rtn = roleService.add(role);
		
		//根据操作结果跳转到不同的页面
		if(rtn > 0){
			modelAndView.setViewName("redirect:/role/roles");
		}else{
			modelAndView.addObject("msg", "保存角色失败");
			modelAndView.setViewName("forward:/WEB-INF/views/role/add.jsp");
		}
		return modelAndView;
	}

	/**
	 * 获取角色列表
	 */
	@RequestMapping("/roles{index}")
	public ModelAndView list(String sname,
			@PathVariable(value = "index", required = false) Integer index,
			ModelAndView modelAndView) {
		
		//如果有角色名称作为关键字，就设置一下
		Sysrole role = new Sysrole();
		if(sname!=null && !sname.equals("")){
			role.setRolename("%" + sname + "%");
		}
		
		//设置分页逻辑所需参数
		PageControl pc = new PageControl();
		Integer currindex = 1;
		if(index != null){
			currindex = index;
		}
		pc.setCurrentindex(currindex);
		pc.setPagesize(5);
		Pager<Sysrole> pager = roleService.list(role, pc);
		
		//将结果集和分页逻辑封装到request域中
		modelAndView.addObject("pager", pager);
		
		//设置跳转逻辑
		modelAndView.setViewName("forward:/WEB-INF/views/role/list.jsp");
		
		return modelAndView;
	}
}
